#!/bin/env ruby
# encoding: utf-8
class TerminalUser < ActiveRecord::Base
  has_paper_trail :version => :paper_trail_version, :versions => :paper_trail_versions
  has_one :terminal_order

  validates_presence_of :industry
  #validates_presence_of :car_license,:driver_name,:driver_phone,:if => Proc.new {|t| t.terminal_order.name == 'yidong' },:on => :update
  #validates_presence_of :user_unit,:name,:phone,:address,:if => Proc.new{|t| t.terminal_order.name == 'zengqiang'},:on => :update

  after_save :driver_account_name_init

  private

  def driver_account_name_init
    if account = Account.where(mobile_number: driver_phone).first
      account.update_column(:name, self.driver_name)
    else
      Account.create(mobile_number: driver_phone, name: self.driver_name)
    end

    if account = Account.where(mobile_number: driver_phone2).first
      account.update_column(:name, self.driver_name2)
    else
      Account.create(mobile_number: driver_phone2, name: self.driver_name2)
    end

  end

end
